var parentData, parentActive;
var dateFormatStr = 'yyyy-MM-dd HH:mm:ss';
layui.use(['form', 'table', 'layer', 'laydate'], function () {
	var form = layui.form,
		layer = layui.layer,
		table = layui.table,
		laydate = layui.laydate,
		$ = layui.$;

	var imgArtwork, imgUrl;
	window.active = {
		reload: () => {
			parentActive.reload();
		}
	};
	//操作
	$('.layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//表格头部按钮操作
	table.on('toolbar(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});
	//表格按钮操作
	table.on('tool(tableList)', function (obj) {
		var type = obj.event;
		active[type] ? active[type].call(this, obj.data) : '';
	});


	//延时初始化，确保数据已经塞进来了
	setTimeout(() => {
		//执行一个laydate实例
		laydate.render({
			elem: '#dt1', //指定元素
			type: 'datetime',
			trigger: 'click',
			// min: js.format_datetime(new Date(), dateFormatStr),
			value: js.format_datetime(parentData && parentData.dt1 || new Date(), dateFormatStr),
		});

		//执行一个laydate实例
		laydate.render({
			elem: '#dt2', //指定元素
			type: 'datetime',
			trigger: 'focus',
			// min: js.format_datetime(new Date((new Date()).getTime() + 1000 * 60 * 60 * 24), dateFormatStr),
			value: js.format_datetime(parentData && parentData.dt2 || new Date((new Date()).getTime() + 1000 * 60 * 60 * 24), dateFormatStr),
		});
		if (!parentData) return;
		//初始化数据
		form.val("dataform", JSON.parse(JSON.stringify(parentData)));
		if (parentData.img) {
			imgUrl = parentData.img;
			$(".pic-cont").css('background-size', 'cover');
			$(".pic-cont").css('background-image', `url(${parentData.img})`);
		}
	}, 200);

	form.verify({});

	form.on("submit(save)", element => {
		
		if (js.isNullOrUndefined(imgArtwork) && js.isNullOrUndefined(imgUrl)) {
			top.layer.msg('请上传头像', {
				icon: 2,
				time: 1000 //2秒关闭（如果不配置，默认是3秒）
			});
		}
		(async function () {
			let data = form.val("dataform");
			// console.log(data);
			if (!js.isNullOrUndefined(imgArtwork) && imgUrl != imgArtwork.path) {
				const formData = new FormData();
				var nameImg = new Date().getTime() + '.jpg';
				formData.append('file', imgArtwork, nameImg);
				let res = await ajaxAsyncNoTip('upload', formData, 'POST', () => { }, { // 请求后台接口
					processData: false,
					contentType: false
				});
				if (res.upload_files.length) {
					let resImg = res.upload_files[0];
					if (resImg.code != 0) {
						top.layer.alert(resImg.msg + ":" + resImg.info);
						return;
					}
					else {
						imgUrl = resImg.data.imgPath;
					}
				} else {
					top.layer.alert('上传头像失败');
					return;
				}
			}
			data.img = imgUrl;
			data.dt1 = $('#dt1').val();
			data.dt2 = $('#dt2').val();
			// popSubmit('gameLottery', data);
			let res= await ajaxAsync('gameLottery', data, "PUT")
			console.log(res,data);
			if(data.id==''){
				data.id=res
				popEdit(data, 'page/gameLottery/editPrize.html', ["750px", "580px"]) 
			}else{
				popEdit(data, 'page/gameLottery/editPrize.html', ["750px", "580px"]) 
			}
			parentActive.reload(); //刷新父页面
			if(___popLayerIndex)
				top.layer.close(___popLayerIndex);
			else
				top.layer.closeAll('iframe'); //关闭 
		})();
		return false;
	});

	$('.pic-cont').click(function () {
		document.getElementById('J-update-img-file').click();
	});
	$('#J-update-img-file').on('change', async function (e) {
		if (!this.files[0] || !window.FileReader) return;
		if (this.files[0].type !== 'image/jpeg') {
			alert('请选择jpg格式的图片!');
			return;
		}

		imgArtwork = await ci.compressImg(this.files[0], {
			size: 0.5,
			Rectangle: true
		});
		imgArtwork.path = URL.createObjectURL(imgArtwork);
		imgArtwork.name = this.files[0].name;
		// $(".J-user-header-img").attr('src', imgUrl);
		$(".pic-cont").css('background-size', 'cover');
		$(".pic-cont").css('background-image', `url(${imgArtwork.path})`);
		return;
	});
});